<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit"
	 :showHistoryBtn="true">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>
<script>
	import {
		getDateStr,
		errorModal,
		ignoreHtml,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		configSelectData,
		getFormFields,
		buildFormData,
		formValidation,
		changeEditor,
		formShowLoading,
		configFormEditor,
		getWorkCacheKey,
		setSubmitSubValue,
		changeConfigAttr,
		formModelLoad,
		hasEditor,
		getEditorTitle
	} from '@/static/js/formUtil.js';
	import dateTimePicker from '@/static/js/dateTimePicker.js';
	import moment from "@/static/js/moment.js"
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				menuName: '',
				busCode: '',
				formdata: {},
				mainConfigData: [],
				subConfigData: {},
				initValue: {},
				subEditorObj: {}, //存放子表编辑器数据
				adressCacheKey: "obj-w-userdaily-adress-cache"
			});
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods: {
			verifyHealth() {
				let _that = this
				let editor = _that.$refs.autoform.getEditor("title_basic", "addressId")
				if (!_that.$refs.autoform.formdata.addressId) {
					_that.om.toast("请先选择" + editor.title, 3000)
					return false
				} else {
					return true
				}
			},
			onSelectDate(date) {
				let _that = this
				if (_that.subEditorObj["title_work"]) {
					_that.$refs.autoform.changeConfigAttr("title_work", "title", date + _that.subEditorObj["title_work"].title)
				}
				if (_that.subEditorObj["title_plan"]) {
					let time = new Date(date).getTime()
					_that.$refs.autoform.changeConfigAttr("title_plan", "title", moment(time).add(1,"day").format("YYYY-MM-DD") + _that.subEditorObj["title_plan"].title)
				}
				if(_that.$refs.autoform.hasEditor("title_lastWorkPlan","lastWorkPlan")){
					let time = new Date(date).getTime()
					_that.$refs.autoform.changeConfigAttr("title_lastWorkPlan", "title", moment(time).format("YYYY-MM-DD") +(_that.$refs.autoform.getEditorTitle("title_lastWorkPlan","lastWorkPlan")).substring(1))
				}
				_that.om.request(
					'tuserDailyReport/getTitleNameByDate',
					{
						date: date
					}
				).then(res=>{
					if(res.value.lastWorkPlan){
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"lastWorkPlan",res.value.lastWorkPlan)
						_that.$refs.autoform.changeEditor("title_lastWorkPlan","lastWorkPlan","disabled",true)
						_that.$refs.autoform.changeEditor("title_lastWorkPlan","lastWorkPlan","showClear",false)
					}else{
						_that.$refs.autoform.clear("title_lastWorkPlan","lastWorkPlan")
					}
					if(_that.$refs.autoform.hasEditor("title_remark","remark")){
						_that.$refs.autoform.changeConfigAttr("title_remark", "title", res.value.rename_remark)
					}
					if(_that.$refs.autoform.hasEditor("title_outing","outing")){
						_that.$refs.autoform.changeConfigAttr("title_outing", "title", res.value.rename_outing)
					}
					if(_that.$refs.autoform.hasEditor("title_workPlan","workPlan")){
						_that.$refs.autoform.changeConfigAttr("title_workPlan", "title", res.value.rename_workPlan)
					}
				})
			},
			formSubmit() {
				let _that = this;
				var checkRes = formValidation(this.$refs.autoform.configData, this.formdata);
				if (checkRes) {
					this.om.toast(checkRes);
				} else {
					//子表提交数据赋值
					setSubmitSubValue()
					if (this.subEditorObj['title_work'] && this.subEditorObj['title_plan'] && !_that.formdata.objStr && !_that.formdata.objStr2) {
						this.om.toast(this.subEditorObj['title5'].title + '和' + this.subEditorObj['title_plan'].title + '不能同时为空');
						return;
					}
					_that.om.formSave('tuserDailyReport/addTuserDailyReportByH5', _that.formdata, _that)
				}
			},
			initLoad(fileId) {
				let _that = this;
				if (!this.busCode) {
					return;
				}
				var ajaxArr = [{
						url: "tsysdict/getComboTree",
						param: {
							code: '38129'
						}
					},
					{
						url: 'tsysdict/getComboTree',
						param: {
							code: '38138'
						}
					},
					{
						url: 'tsysdict/getListByCode',
						param: {
							code: '38612'
						}
					},
					{
						url: 'tuserDailyReport/getTitleNameByDate',
						param: {
							date: getDateStr(new Date())
						}
					}
				]
				if (fileId) {
					ajaxArr.push({
						url: "tuserDailyReport/getInfo",
						param: {
							id: fileId
						}
					})
				}
				formModelLoad(ajaxArr, function(res, mainData) {
					changeEditor("topinfo", "zuserId", {
						defaultValue: _that.userId,
						defaultName: _that.userName
					}, null, mainData)
					changeEditor("title_basic", "healthId", "data", configSelectData(res[0], "id", "text", true), mainData)
					changeEditor("title_basic", "cateId", "data", configSelectData(res[1], "id", "text", true), mainData)
					changeEditor("title_basic", "healthyId", "data", configSelectData(res[2], "id", "dicname"), mainData)
					changeEditor("topinfo", "date", {
						defaultValue: dateTimePicker.getNewDate(),
						defaultName: dateTimePicker.getNewDate()
					}, null, mainData)
					changeEditor("title_basic", "addressId",{
						link: "../../public/pageSearch/pageSearch",
						"linkParam": {
							other: JSON.stringify({}),
							url: 'regions/getAddressCombox',
							searchfield: "q",
							namefield: 'fullName',
							cachename: _that.adressCacheKey,
							placeholder:"请输入城市"
						}
					}, null, mainData)
					if (_that.subConfigData['title_work']) {
						if(fileId){
							changeConfigAttr("title_work", "title",res[4].value.list1Title,
								mainData)
						}else{
							changeConfigAttr("title_work", "title", moment().format('YYYY-MM-DD') + _that.subEditorObj["title_plan"].title,
								mainData)
						}
						var configData = _that.subConfigData['title_work']
						var attr = {}
						if (hasEditor('title_work', 'startTime', configData)) {
							attr.titleTemplate = '<%= startTime %>'
						}
						if (hasEditor('title_work', 'endTime', configData)) {
							attr.titleTemplate = '<%= endTime %>'
						}
						if (hasEditor('title_work', 'startTime', configData) && hasEditor('title_work', 'endTime', configData)) {
							attr.titleTemplate = '<%= startTime %> ~ <%= endTime %>'
						}
						if (hasEditor('title_work', 'placeId', configData)) {
							attr.subtitle3Template = '<%= placeName %>'
						}
						if (hasEditor('title_work', 'street', configData)) {
							attr.subtitle1Template = '<%= street %>'
						}
						if (hasEditor('title_work', 'vehicleCode', configData)) {
							attr.subtitle2Template = '<%= vehicleName %>'
						}
						changeEditor('title_work', 'objStr', attr, mainData);
					}
					if (_that.subEditorObj["title_plan"]) {
						if(fileId){
							changeConfigAttr("title_plan", "title", res[4].value.list2Title,
								mainData)
						}else{
							changeConfigAttr("title_plan", "title", moment().add(1,'day').format('YYYY-MM-DD') + _that.subEditorObj["title_plan"].title,
								mainData)
						}
						if (_that.subConfigData['title_plan']) {
							var configData = _that.subConfigData['title_plan']
							var attr = {}
							if (hasEditor('title_plan', 'startTime', configData)) {
								attr.titleTemplate = '<%= startTime %>'
							}
							if (hasEditor('title_plan', 'endTime', configData)) {
								attr.titleTemplate = '<%= endTime %>'
							}
							if (hasEditor('title_plan', 'startTime', configData) && hasEditor('title_plan', 'endTime', configData)) {
								attr.titleTemplate = '<%= startTime %> ~ <%= endTime %>'
							}
							if (hasEditor('title_plan', 'placeId', configData)) {
								attr.subtitle3Template = '<%= placeName %>'
							}
							if (hasEditor('title_plan', 'street', configData)) {
								attr.subtitle1Template = '<%= street %>'
							}
							if (hasEditor('title_plan', 'vehicleCode', configData)) {
								attr.subtitle2Template = '<%= vehicleName %>'
							}
							changeEditor('title_plan', 'objStr2', attr, mainData);
						}
					}
					if(hasEditor("title_lastWorkPlan","lastWorkPlan",mainData)){
						changeEditor("title_lastWorkPlan","lastWorkPlan","disabled",true,mainData)
						changeConfigAttr("title_lastWorkPlan", "title", moment().subtract(1,'day').format('YYYY-MM-DD') + (getEditorTitle("title_lastWorkPlan","lastWorkPlan",mainData)).substring(1),
							mainData)
						changeEditor("title_lastWorkPlan","lastWorkPlan","showClear",false,mainData)
						changeEditor("title_lastWorkPlan","lastWorkPlan","prompt","",mainData)
						if(res[3].value.lastWorkPlan && !fileId){
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"lastWorkPlan",res[3].value.lastWorkPlan,mainData)
						}
					}
					if(hasEditor("title_remark","remark",mainData)){
						changeConfigAttr("title_remark", "title", res[3].value.rename_remark ,
							mainData)
					}
					if(hasEditor("title_outing","outing",mainData)){
						changeConfigAttr("title_outing", "title", res[3].value.rename_outing ,
							mainData)
					}
					if(hasEditor("title_workPlan","workPlan",mainData)){
						changeConfigAttr("title_workPlan", "title", res[3].value.rename_workPlan,
							mainData)
					}
					if (fileId) {
						var fielArr = getFormFields(mainData)
						fielArr.push("id")
						for (var item in res[4].value.tuserDailyReport) {
							if (fielArr.includes(item)) {
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata, item, res[4].value.tuserDailyReport[item])
								_that.$set(_that.formdata, item, res[4].value.tuserDailyReport[item])

							}
						}
						if(res[4].value.lastWorkPlan){
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"lastWorkPlan",res[4].value.lastWorkPlan,mainData)
						}
						if(hasEditor("title_basic","healthId",mainData)){
							for (let item of res[0]) {
								if (_that.$refs.autoform.formdata.healthId.indexOf(item.id) != -1) {
									_that.$refs.autoform.$set(item, "checked", true)
								}
							}
						}
						if(hasEditor("title_basic","cateId",mainData)){
							for (let item of res[1]) {
								if (_that.$refs.autoform.formdata.cateId.indexOf(item.id) != -1) {
									_that.$refs.autoform.$set(item, "checked", true)
								}
							}
						}
						if (_that.subEditorObj["title_work"]) {
							let dataList = _that._.clone(res[4].value.list1)
							_that.subEditorObj["title_work"].workList = dataList
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata, "objStr", JSON.stringify(dataList))
							_that.$set(_that.formdata, "objStr", JSON.stringify(dataList))
							delete res[4].value.dataList
						}      
						if (_that.subEditorObj["title_plan"]) {
							let planList = _that._.clone(res[4].value.list2)
							_that.subEditorObj["title_plan"].workList = planList
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata, "objStr2", JSON.stringify(planList))
							_that.$set(_that.formdata, "objStr2", JSON.stringify(planList))
							delete res[4].value.planList
						}
						_that.$nextTick(function() {
							_that.$refs.autoform.initSelectIndex("title_basic", "healthyId")
							if(fileId){
								if(_that.$refs.autoform.hasEditor("title_remark","remark")){
									_that.$refs.autoform.changeConfigAttr("title_remark","title",res[4].value.rename_remark)
								}
								if(_that.$refs.autoform.hasEditor("title_outing","outing")){
								_that.$refs.autoform.changeConfigAttr("title_outing","title",res[4].value.rename_outing)
								}
								if(_that.$refs.autoform.hasEditor("title_workPlan","workPlan")){
								_that.$refs.autoform.changeConfigAttr("title_workPlan","title",res[4].value.rename_workPlan)
								}
								if(_that.$refs.autoform.hasEditor("title_lastWorkPlan","lastWorkPlan")){
								_that.$refs.autoform.changeConfigAttr("title_lastWorkPlan","title",res[4].value.rename_lastWorkPlan)
								}
							}
						})
					}

				})
			},
		},
		onLoad(options) {
			let _that = this;
			_that.menuName = options.title || '日报';
			uni.setNavigationBarTitle({
				title: _that.menuName
			});
			formShowLoading(_that)
			_that.busCode = options.code || "OBJ0603";
			_that.formdata.userId = this.userId;
			_that.initLoad(options.fileId);
		},
		onUnload() {
			//#ifdef APP-PLUS
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					uni.removeStorage({
						key: getWorkCacheKey(fieldId)
					});
				}
			}
			//#endif
		},
		onShow() {
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					let editor = _that.subEditorObj[fieldId]
					uni.getStorage({
						key: getWorkCacheKey(fieldId),
						success: function(res) {
							editor.workList = res.data;
							uni.removeStorage({
								key: getWorkCacheKey(fieldId)
							});
						}
					});
				}
				uni.getStorage({
					key: _that.adressCacheKey,
					success: (res) => {
						if (res.data) {
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.addressId = d.id
							formdata.addressName = d.fullName
							uni.removeStorage({
								key: _that.adressCacheKey
							})
						}
					}
				})
			}
		}
	};
</script>
<style></style>
